package com.hoperun.checkcloth.fragment;

import android.app.Dialog;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

import com.hoperun.checkcloth.R;
import com.hoperun.checkcloth.util.LanguageUtil;

public class CountDialogFragment extends DialogFragment implements OnClickListener {

    private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, deleteBtn, okBtn;
    private TextView resultTv;
    private final static String RESULT = "result";

    private int mResult = 0;

    public static CountDialogFragment newInstance(int result) {
        CountDialogFragment dialog = new CountDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(RESULT, result);
        dialog.setArguments(bundle);// 把参数传递给该DialogFragment
        return dialog;
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_counter_layout, null);
        Dialog dialog = new Dialog(getActivity(), R.style.Mip_DialogStyle);
        dialog.setCanceledOnTouchOutside(true);// 点击边际可消失
        dialog.setContentView(view);
        initViews(view);
        initDates();
        return dialog;
    }

    private void initViews(View view) {
        btn0 = view.findViewById(R.id.btn0);
        btn1 = view.findViewById(R.id.btn1);
        btn2 = view.findViewById(R.id.btn2);
        btn3 = view.findViewById(R.id.btn3);
        btn4 = view.findViewById(R.id.btn4);
        btn5 = view.findViewById(R.id.btn5);
        btn6 = view.findViewById(R.id.btn6);
        btn7 = view.findViewById(R.id.btn7);
        btn8 = view.findViewById(R.id.btn8);
        btn9 = view.findViewById(R.id.btn9);
        okBtn = view.findViewById(R.id.okBtn);
        deleteBtn = view.findViewById(R.id.deleteBtn);
        resultTv = view.findViewById(R.id.resultTv);
        btn0.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        okBtn.setOnClickListener(this);
        deleteBtn.setOnClickListener(this);

        //判断是越南文的时候缩小字体
        if (LanguageUtil.getLanguageStr().equals("vi")) {
            okBtn.setTextSize(20);
            deleteBtn.setTextSize(20);
        }

    }

    private void initDates() {
        mResult = getArguments().getInt(RESULT);
        resultTv.setText(String.valueOf(mResult));
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn0:
                calculate(0);
                break;
            case R.id.btn1:
                calculate(1);
                break;
            case R.id.btn2:
                calculate(2);
                break;
            case R.id.btn3:
                calculate(3);
                break;
            case R.id.btn4:
                calculate(4);
                break;
            case R.id.btn5:
                calculate(5);
                break;
            case R.id.btn6:
                calculate(6);
                break;
            case R.id.btn7:
                calculate(7);
                break;
            case R.id.btn8:
                calculate(8);
                break;
            case R.id.btn9:
                calculate(9);
                break;
            case R.id.deleteBtn:
                delete();
                break;
            case R.id.okBtn:
                if (mOkClickListener != null) {
                    mOkClickListener.onClick(mResult);
                }
                break;
            default:
                break;
        }
    }

    private onOKClickListener mOkClickListener;

    public interface onOKClickListener {
        void onClick(int result);
    }

    public void setOnOKClickListener(onOKClickListener onOKClickListener) {
        mOkClickListener = onOKClickListener;
    }

    /**
     * 计算并显示新的数值
     */
    private void calculate(int num) {
        try {
            if (mResult == 0) {
                mResult = num;
            } else {
                mResult = mResult * 10 + num;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        resultTv.setText(String.valueOf(mResult));
    }

    /**
     * 删除
     */
    private void delete() {
        try {
            mResult = mResult / 10;
        } catch (Exception e) {
            e.printStackTrace();
        }
        resultTv.setText(String.valueOf(mResult));
    }

}
